package org.dromara.authcode.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.authcode.domain.bo.TPayTransBo;
import org.dromara.authcode.domain.vo.TPayTransVo;
import org.dromara.authcode.domain.TPayTrans;
import org.dromara.authcode.mapper.TPayTransMapper;
import org.dromara.authcode.service.ITPayTransService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 支付流水Service业务层处理
 *
 * @author Miles Meng
 * @date 2025-08-08
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TPayTransServiceImpl implements ITPayTransService {

    private final TPayTransMapper baseMapper;

    /**
     * 查询支付流水
     *
     * @param transId 主键
     * @return 支付流水
     */
    @Override
    public TPayTransVo queryById(Long transId){
        return baseMapper.selectVoById(transId);
    }

    /**
     * 分页查询支付流水列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 支付流水分页列表
     */
    @Override
    public TableDataInfo<TPayTransVo> queryPageList(TPayTransBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TPayTrans> lqw = buildQueryWrapper(bo);
        Page<TPayTransVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的支付流水列表
     *
     * @param bo 查询条件
     * @return 支付流水列表
     */
    @Override
    public List<TPayTransVo> queryList(TPayTransBo bo) {
        LambdaQueryWrapper<TPayTrans> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TPayTrans> buildQueryWrapper(TPayTransBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TPayTrans> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TPayTrans::getTransId);
        lqw.eq(bo.getTransDate() != null, TPayTrans::getTransDate, bo.getTransDate());
        lqw.eq(bo.getTransTime() != null, TPayTrans::getTransTime, bo.getTransTime());
        lqw.eq(bo.getAmt() != null, TPayTrans::getAmt, bo.getAmt());
        lqw.eq(StringUtils.isNotBlank(bo.getCurrency()), TPayTrans::getCurrency, bo.getCurrency());
        lqw.eq(StringUtils.isNotBlank(bo.getTransStatus()), TPayTrans::getTransStatus, bo.getTransStatus());
        lqw.eq(bo.getOrderNo() != null, TPayTrans::getOrderNo, bo.getOrderNo());
        lqw.eq(StringUtils.isNotBlank(bo.getPayChannel()), TPayTrans::getPayChannel, bo.getPayChannel());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelMchNo()), TPayTrans::getChannelMchNo, bo.getChannelMchNo());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelTransId()), TPayTrans::getChannelTransId, bo.getChannelTransId());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelExtra()), TPayTrans::getChannelExtra, bo.getChannelExtra());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelUserId()), TPayTrans::getChannelUserId, bo.getChannelUserId());
        lqw.eq(bo.getUserId() != null, TPayTrans::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getPayWay()), TPayTrans::getPayWay, bo.getPayWay());
        lqw.eq(StringUtils.isNotBlank(bo.getIsReturnNotify()), TPayTrans::getIsReturnNotify, bo.getIsReturnNotify());
        lqw.eq(bo.getReturnNotifyTime() != null, TPayTrans::getReturnNotifyTime, bo.getReturnNotifyTime());
        lqw.eq(bo.getSuccTime() != null, TPayTrans::getSuccTime, bo.getSuccTime());
        return lqw;
    }

    /**
     * 新增支付流水
     *
     * @param bo 支付流水
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TPayTransBo bo) {
        TPayTrans add = MapstructUtils.convert(bo, TPayTrans.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTransId(add.getTransId());
        }
        return flag;
    }

    /**
     * 修改支付流水
     *
     * @param bo 支付流水
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TPayTransBo bo) {
        TPayTrans update = MapstructUtils.convert(bo, TPayTrans.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TPayTrans entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除支付流水信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
